
/**
 * @(#)ConnectFourLauncher.java
 *
 *
 * @author
 * @version 1.00 2012/3/8
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ConnectFourLauncher implements ActionListener{

    JFrame window;
    ConnectFourBoard board;
    JButton Flip=new JButton("Flip");
    Timer t;
    boolean flip=false;
    /**
     * Creates a new instance of <code>ConnectFourLauncher</code>.
     */
    public ConnectFourLauncher(int col, int row, int res, Boolean flip, Boolean remove, int time,Boolean isHost,Boolean isServer,
    		String portstr, String serverstr)
    {
    	char rules;
    	if(flip&&remove)
    		rules='b';
    	else if(flip)
    		rules='f';
    	else if(remove)
    		rules='r';
    	else
    		rules='n';
    	
    	window = new JFrame("Connect Four");
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	board = new ConnectFourBoard(row,col,res,flip,remove,time);
    	
    	//instantiate the timer
    	if(time!=0)
    	{
	    	ActionListener task = new ActionListener()
	    			{
		    			public void actionPerformed(ActionEvent evt) 
		    			{
		    				int loser=board.core.GetCurrentPlayer();
		    				if(loser==1)
		    					winnerWindow(2,true);
		    				else
		    					winnerWindow(1,true);
		    			}
	    			};
	    			
	    	t=new Timer(time,task);
	    	t.setRepeats(false);
    	}

    	
    	Flip.setAlignmentX(Component.RIGHT_ALIGNMENT);
    	if(flip)
    		Flip.addActionListener(this);

    	Container content = window.getContentPane();
    	content.setLayout(new BorderLayout());

    	JPanel controls = new JPanel();
    	controls.setPreferredSize(new Dimension(100,row*res));
    	controls.setBorder(new BevelBorder(BevelBorder.LOWERED));
    	if(flip)
    		controls.add(Flip);
    	window.add(controls,BorderLayout.EAST);
    	
    	JPanel statusPanel = new JPanel();
    	statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
    	window.add(statusPanel, BorderLayout.SOUTH);
    	
    	statusPanel.setPreferredSize(new Dimension(window.getWidth(), 20));
    	statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    	JLabel statusLabel = new JLabel("Player"+Integer.toString(board.core.GetCurrentPlayer()));
    	statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
    	statusPanel.add(statusLabel);
    	
    	content.add(board);
   
		window.setResizable(false);
    	window.pack();
    	window.setVisible(true);
    	boolean inProgress=true;
    	if(isHost)
    	{
    		Server server= new Server(Integer.parseInt(portstr));
    		server.setGame(board.core);
    		
    		if(server.ackClientConnection())
    		{
    			server.sendClientGameInfo(row, col, 1, time,rules);
    			server.sendClientGameStart();
    			while(inProgress)
    			{
    				//server.sendClientMove(move, col)
    				
    			}
    		}
    	}
    	/*if(isClient)
    	{
    		
    	}*/
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new TitleWin() ;
    }

	public void actionPerformed(ActionEvent a) 
	{
		if(a.getSource()==Flip)
		{
			board.Flip();
			flip=true;
		}
	}
	public void winnerWindow(int winnerInt, Boolean timeloss)
    {
    	
    	board.removeMouseListener(board);
    	
    	//did the player lose by running out of time?
		String display;
		if(timeloss==false)
			display= "Player "+Integer.toString(winnerInt)+" has won!";
		else 
		{
			//stop timer
	    	t.stop();
			display="Player "+Integer.toString(winnerInt)+" has won (by time limit)!";
		}
		
		JOptionPane.showMessageDialog(board, display, "Winner", JOptionPane.INFORMATION_MESSAGE);
		
    	//game logic for playing again
    	Object[] options = {"Yes",
        "No"};

		int n = JOptionPane.showOptionDialog(board,
				"Would you like to play again?",
						"Play Again?",
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE,
						null,
						options,
						options[1]);
		if(n==1)
		{
			System.exit(0);
		}
		else
		{
			window.dispose();
			new SizingWindow();
		}

    }
}
